package com.lovo.csc.config;

import com.lovo.util.MQUtil;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    //spring封装rabiitmq的代码 RabbitTemplate
    //注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }

    //采购标书交换机
    @Bean
    public DirectExchange tenderDirectExchange(){
        return new DirectExchange(MQUtil.tenderDirectExchange);
    }
    //客户注册传后端交换机
    @Bean
    public DirectExchange houExchange(){
        return new DirectExchange(MQUtil.houExchange);
    }
    //客户注册队列
    @Bean
    public Queue houQueue(){
        return new Queue(MQUtil.houQueue);
    }

    //促销交换机
    @Bean
    public DirectExchange PromotionDirectExchange(){
        return new DirectExchange(MQUtil.PromotionDirectExchange);
    }
    //返回取消促销队列
    @Bean
    public Queue PromotionQueue(){
        return new Queue(MQUtil.PromotionQueue);
    }
    //返回审核促销队列
    @Bean
    public Queue PromotionInfoQueue(){
        return new Queue(MQUtil.PromotionInfoQueue);
    }

    //采购标书队列
    @Bean
    public Queue tenderQueue(){
        return new Queue(MQUtil.tenderQueue);
    }

    //采购单交换机
    @Bean
    public DirectExchange PurchaseInfoDirectExchange(){
        return new DirectExchange(MQUtil.PurchaseInfoDirectExchange);
    }
    //采购单返回队列
    @Bean
    public Queue PurchaseInfoQueue(){
        return new Queue(MQUtil.PurchaseInfoQueue);
    }
    @Bean
    public Queue settlementQueue(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", MQUtil.directExchangeDead);
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key",MQUtil.deadKey);
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
        return QueueBuilder.durable(MQUtil.settlementQueue).withArguments(args).build();
    }
    @Bean
    public DirectExchange directExchangeSettlement(){
        return new DirectExchange(MQUtil.directExchangeSettlement);
    }
    //绑定交换机和队列
    @Bean
    public Binding directExchangeOrderToOrderQueue(Queue settlementQueue,DirectExchange directExchangeSettlement){
        return BindingBuilder.bind(settlementQueue).to(directExchangeSettlement).with(MQUtil.settlementKey);
    }


    @Bean
    public Queue deadQueue(){
        return new Queue(MQUtil.deadQueue);
    }
    @Bean
    public DirectExchange directExchangeDead(){
        return new DirectExchange(MQUtil.directExchangeDead);
    }

    @Bean
    public Binding directExchangeDeadToDeadQueue(Queue deadQueue,DirectExchange directExchangeDead){
        return BindingBuilder.bind(deadQueue).to(directExchangeDead).with(MQUtil.deadKey);
    }
    @Bean
    //连接采购标书交换机和队列
    public Binding tenderDirectExchangeToTenderQueue(DirectExchange tenderDirectExchange,Queue tenderQueue){
        return BindingBuilder.bind(tenderQueue).to(tenderDirectExchange).with(MQUtil.tenderKey);
    }
    @Bean
    //连接采购单返回交换机和队列
    public Binding PurchaseInfoDirectExchangeToPurchaseInfoQueue(DirectExchange PurchaseInfoDirectExchange,Queue PurchaseInfoQueue){
        return BindingBuilder.bind(PurchaseInfoQueue).to(PurchaseInfoDirectExchange).with(MQUtil.PurchaseInfoKey);
    }
    @Bean
    //连接客户注册交换机和队列
    public Binding houExchangeTohouQueue(DirectExchange houExchange,Queue houQueue){
        return BindingBuilder.bind(houQueue).to(houExchange).with(MQUtil.houKey);
    }
    // 预存款消费成功通知队列 王孝亮
    @Bean
    public Queue ExpenditureQueue(){
        return new Queue(MQUtil.ExpenditureQueue);
    }
    //预存款成功的通知队列， 刘虹刚
    @Bean
    public Queue DepositQueue(){
        return new Queue(MQUtil.DepositQueue);
    }
    @Bean
    public DirectExchange PrepayExchange(){// 预存款专用交换机
        return new DirectExchange(MQUtil.PrepayExchange);
    }
    @Bean
    public Binding DepositQueueToPrepayExchange(Queue DepositQueue,DirectExchange PrepayExchange){
        return BindingBuilder.bind(DepositQueue).to(PrepayExchange).with(MQUtil.depositKey);
    }
    @Bean
    public Binding ExpenditureQueueToPrepayExchange(Queue ExpenditureQueue,DirectExchange PrepayExchange){
        return BindingBuilder.bind(ExpenditureQueue).to(PrepayExchange).with("expenditure.key");
    }
    //  预存款消费成功给刘虹刚
    @Bean
    public Queue ExpenseQueue(){
        return new Queue(MQUtil.ExpenseQueue);
    }
    @Bean
    public Binding ExpenseQueueToPrepayExchange(Queue ExpenseQueue,DirectExchange PrepayExchange){
        return BindingBuilder.bind(ExpenseQueue).to(PrepayExchange).with("expense.key");
    }
    @Bean
    public Queue PrepayOrderQueue(){
        return new Queue(MQUtil.PrepayOrderQueue);
    }
    // 给后端的消费成功队列加绑定
    @Bean
    public Binding PrepayOrderQueueToPrepayExchange(Queue PrepayOrderQueue,DirectExchange PrepayExchange){
        return BindingBuilder.bind(PrepayOrderQueue).to(PrepayExchange).with("inform.key");
    }
    @Bean
    public FanoutExchange fanoutExchangePrepay(){
        return new FanoutExchange("fanoutExchangePrepay");
    }
    @Bean
    public Queue ReturnInformQueue(){
        return new Queue(MQUtil.ReturnInformQueue);
    }
    @Bean
    public Binding ReturnInformQueueToPrepayExchange(Queue ReturnInformQueue,DirectExchange PrepayExchange){
        return BindingBuilder.bind(ReturnInformQueue).to(PrepayExchange).with("return.key");
    }
    @Bean
    public Queue ReturnQueueBack(){
        return new Queue(MQUtil.ReturnQueueBack);
    }
    @Bean
    public Queue ReturnQueueAfter(){
        return new Queue(MQUtil.ReturnQueueAfter);
    }
    @Bean
    public Binding fanoutExchangeToReturnQueueBack( Queue ReturnQueueBack,FanoutExchange fanoutExchangePrepay){
        return BindingBuilder.bind(ReturnQueueBack).to(fanoutExchangePrepay);
    }
    @Bean
    public Binding fanoutExchangeToReturnQueueAfter( Queue ReturnQueueAfter,FanoutExchange fanoutExchangePrepay){
        return BindingBuilder.bind(ReturnQueueAfter).to(fanoutExchangePrepay);
    }

    //连接取消促销交换机和队列
    @Bean
    public Binding PromotionDirectExchangeToPromotionQueue(DirectExchange PromotionDirectExchange,Queue PromotionQueue){
        return BindingBuilder.bind(PromotionQueue).to(PromotionDirectExchange).with(MQUtil.PromotionKey);
    }
    //连接促销审核交换机和队列
    @Bean
    public Binding PromotionDirectExchangeToPromotionInfoQueue(DirectExchange PromotionDirectExchange,Queue PromotionInfoQueue){
        return BindingBuilder.bind(PromotionInfoQueue).to(PromotionDirectExchange).with(MQUtil.PromotionInfoKey);
    }
}